package org.igeek.atomqq.task;

import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;

import org.igeek.atomqq.activity.BaseActivity;
import org.igeek.atomqq.domain.Group;
import org.igeek.atomqq.net.HttpConnection;
import org.igeek.atomqq.net.HttpConnection.Request_TYPE;
import org.igeek.atomqq.net.Response;
import org.igeek.atomqq.util.URLManager;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;


/**
 * 获取群公告信息
 * 正常返回类型: application/json
 * 
 * GET application/json	NO.	Starred	Timeline	Method	Result	Received	Type	URL	RedirectURL
105	False		GET	200	2.38 K	application/json	http://s.web2.qq.com/api/get_group_info?gcode=%5B2248474822%2C2935293683%2C1664496215%2C2476894861%2C3961240488%2C480541026%2C1149714166%2C2193033000%2C2258876834%2C3217348538%2C3041150958%2C3355848117%2C1657190591%2C1473140354%2C3652241498%2C19476313%2C834964785%2C2588779598%2C2342732287%2C815035306%2C1007031081%2C3907976057%2C3685042388%2C2214389229%2C1154971778%2C711840211%2C1984761473%2C46009036%2C2468983583%2C1429891874%2C3632983260%2C1906349295%2C3639156032%2C3570890122%2C4045969604%2C1929054350%2C184862409%2C1480049686%2C3865162879%5D&retainKey=memo%2Cgcode&vfwebqq=令牌	
 * @author hangxin1940@gmail.com
 * @version 创建时间：2012-1-29 下午1:32:26
 * 
 */
public class GroupMemoTask extends CachedAsyncTask<Void,Void,Void>{
	public static final int ERROR=0x5028;
	
	private TaskResultListener listener;
	private GroupMemoResponse response;
	private long code;
	private int flag;
	
	private Handler handler=new Handler(){
		
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case ERROR:
				activity.showDialog(ERROR, msg.getData());
				break;
			default:
				activity.showDialog(BaseActivity.DIALOG_ERROR, msg.getData());
				break;
			}
		}
	};
	
	private BaseActivity activity;
	
	public GroupMemoTask(BaseActivity activity){
		this.activity=activity;
	}
	
	public void setOnTaskResult(TaskResultListener listener,int flag){
		this.listener=listener;
		this.flag=flag;
	}
	
	public void setParams(long code){
		this.code=code;
	}

	@Override
	protected Void doInBackground(Void... params) {
		
		
		
		try {
			String gcode=URLEncoder.encode("["+code+"]","utf-8");
			String retainKey=URLEncoder.encode("memo,gcode","utf-8");
			String time=new Date().getTime()+"";
			String vfwebqq=HttpConnection.getCookieManager().getCookieValue("vfwebqq");
			
			StringBuffer surl=new StringBuffer(URLManager.GROUP_INFO);
			surl.append("?gcode=");
			surl.append(gcode);
			surl.append("&retainKey=");
			surl.append(retainKey);
			surl.append("&vfwebqq=");
			surl.append(vfwebqq);
			surl.append("&t=");
			surl.append(time);
			URL url = new URL(surl.toString());
			
			HttpConnection conn=new HttpConnection(url, Request_TYPE.GET);
			conn.setReferer(URLManager.REFER_s_web2_qq_com_proxy);
			conn.connect();
			
			Response cesponses=conn.getResponse();
			conn.disconnect();
			
			JSONObject r=cesponses.getJsonObj();
			
			JSONArray result=r.getJSONArray("result");
			
			JSONObject j=result.getJSONObject(0);
			String memo=j.getString("memo");
			
			Group group=new Group();
			group.setMemo(memo);
			group.setCode(code);
			
			response=new GroupMemoResponse();
			response.setGroupMemo(group);
			
		}catch (Exception e) {
			e.printStackTrace();
			showErrorDialog("网络异常",ERROR);
		}
		
		
		return null;
	}

	
	
	private void showErrorDialog(String message,int flag){
		Bundle bundle=new Bundle();
		bundle.putString(BaseActivity.FLAG_ERROR, message);
		Message msg=new Message();
		msg.setData(bundle);
		msg.what=flag;
		handler.sendMessage(msg );
	}
	
	@Override
	protected void onPostExecute(Void result) {
		if(null!=response)
			listener.onTaskResult(response,flag);
	}	

	
	
	public class GroupMemoResponse extends Response{
		private Group groupMemo;

		public Group getGroupMemo() {
			return groupMemo;
		}

		public void setGroupMemo(Group groupMemo)  {
			this.groupMemo=groupMemo;
			
		}
		
		
	}

}
